<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          style="display: none;"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input v-decorator="['id']"/>
        </a-form-item>

        <a-form-item
          label="角色名"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入角色名" v-decorator="['name', {rules: [{required: true, message: '请输入角色名！'}]}]"/>
        </a-form-item>
        <a-form-item
          label="描述信息"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-textarea placeholder="请输入描述信息" :rows="3"
                      v-decorator="['desc', {rules: [{required: false, message: '请输入描述信息！'}]}]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
  import * as api from '@/api/system/roleManage'
  // 导入提示信息字典
  import config from '@/config/dict'

  export default {
    name: 'sForm',
    data() {
      return {
        labelCol: {
          xs: { span: 10 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        title: '',
        confirmLoading: true,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      open(record) {
        this.visible = true
        if (record) {
          this.title = '编辑角色'
        } else {
          this.title = '新增角色'
        }

        if (record) {
          setTimeout(() => {
            this.setUserItem(record)
          }, 100)
        }
        this.confirmLoading = false
      },
      setUserItem(record) {
        this.form.setFieldsValue(
          {
            id: record.id,
            name: record.name,
            desc: record.desc
          }
        )
      },
      // [确认按钮]提交数据方法
      handleSubmit() {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
            this.message = config.msgDict[0]
            if (values.id) {
              this.message = config.msgDict[1]
            }
            if (!errors) {
              this._commitData(values)
            } else {
              this.confirmLoading = false
            }
          }
        )
      },
      // 新增或编辑数据
      _commitData(values) {
        if (values.id) {
          this.message = config.msgDict[1]
          api.updateObj(values)
            .then((res) => {
              this.confirmLoading = false
              this._handle_response(res, values)
            })
            .finally((res) => {
              this.confirmLoading = false
            })
        } else {
          this.message = config.msgDict[0]
          api.addObj(values)
            .then((res) => {
              this.confirmLoading = false
              this._handle_response(res, values)
            })
            .finally((res) => {
              this.confirmLoading = false
            })
        }
      },
      // 处理返回消息
      _handle_response(res, values) {
        if (!res.error) {
          this.$message.success(this.message['success'])
          this.handleCancel()
          this.$emit('ok', values)
        } else {
          this.$message.error(this.message['failed'] + res.error)
        }
      }
      ,
      // [取消按钮]重置数据,关闭弹框
      handleCancel() {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
